<? if (!defined("BASEPATH")) exit("No direct script access allowed");

if(!$this->input->is_ajax_request() and isset($getProgType) and $getProgType) {

	if($ENTITY == "INS") {
		echo	"
					<script type=\"text/javascript\">
						var run = false;
						function jsTabChange(panel) {
							if(run) return;
							$('#ulTabmenu').children().each(function() {
								$(this).removeClass('active');
							});
							$(panel).addClass('active');

							$.ajax({
								type: 'POST',
								url: '".site_url()."main/tabmenu_refresh?' + Math.random(),
								data: {entity: 'INS', url_name: '".$getInstitute[0]["insUrlName"]."', program_type: $(panel).attr('id'), entName: '".$entName."'},
								beforeSend: function(xhr) {
									run = true;
									$('#articleAvailablePrograms').html('<div class=\"box gray\"><p style=\"text-align:center; margin:10px;\"><img src=\"".MEDIA."img/loading.gif\" alt=\"\" /> ".$this->db->escape_str($this->lang->line("PLEASE_WAIT,_PROCESSING_IN_PROGRESS"))."</p><div>');
								}
							}).done(function ( data ) {
									$('#articleAvailablePrograms').html(data);
									run = false;
							});

						}

					</script>
					";
	}

	if(in_array($ENTITY, array("COU", "CITY"))) {
		echo		"<div class=\"content\"><h2 class=\"blue title\">".$this->lang->line("MORE_STUDY_PROGRAM_TYPES_IN")." ".$entName."</h2></div><br/>";
	}

	echo	"<nav class=\"tabmenu\">";
	echo		"<ul id=\"ulTabmenu\">";
	foreach($getProgType as $sname => $szoData) {
		if(in_array($ENTITY, array("COU", "CITY"))) {
			$prefix = "";
			if(in_array($szoData["szoUrlName"], $this->config->item("study-abroad-programs"))) {
				$prefix = "study-abroad/";
			}
			if(isset($getCountry)) {
				$url = $prefix.$szoData["szoUrlName"]."/".$getCountry[0]["conUrlName"]."/".$getCountry[0]["couUrlName"];
			} elseif(isset($getCity)) {
				$url = $prefix.$szoData["szoUrlName"]."/".$getCity[0]["conUrlName"]."/".$getCity[0]["couUrlName"]."/".$getCity[0]["cityUrlName"];
			}
			echo			"<li ".(($selectedProgType == $sname)?("class=\"active\""):(""))." id=\"".$sname."\">".anchor(site_url().$url, "<span>".$szoData["szoLname"]."</span>", array("name" => str_replace(" ", "-", strtolower($szoData["szoLname"]))))."</li>"; // ."/#".str_replace(" ", "-", strtolower($szoData["szoLname"]))
		} else {
			echo			"<li ".(($selectedProgType == $sname)?("class=\"active\""):(""))." id=\"".$sname."\" onclick=\"jsTabChange(this);\"><a href=\"#".str_replace(" ", "-", strtolower($szoData["szoLname"]))."\"><span>".$szoData["szoLname"]."</span></a></li>";
		}
	}
	echo		"</ul>";
	echo	"</nav>";

}

//Common::pr($getAvailablePrograms);

if(isset($getAvailablePrograms["sub"]) and count($getAvailablePrograms["sub"])) {

	if(!$this->input->is_ajax_request()) echo	"<article class=\"content\" id=\"articleAvailablePrograms\">";
	echo		"<h2 class=\"blue title\">".$this->lang->line("AVAILABLE")." ".$getProgType[$selectedProgType]["szoLname"]." ".$this->lang->line("PROGRAMS_IN")." ".$entName."</h2>";

	echo		"<div class=\"accordion\">";

	foreach($getAvailablePrograms["sub"] as $subData) {
		echo			"<article class=\"box\">";

		echo				"<header>";
		echo					"<h3>".$subData["subName"]."</h3>";
		echo					"<dl>";
		echo					"<dt>".$this->lang->line("NUMBER_OF_COURSES").":</dt><dd>".count($subData["majors"])."</dd>";
		echo					"</dl>";
		echo				"</header>";

		echo				"<section>";
		echo					"<table class=\"grid\">";
		$elso = TRUE;
		foreach($subData["majors"] as $majData) {
			if($elso) {
				echo						"<thead>";
				echo							"<tr>";
				if($ENTITY != "INS") {
					echo								"<th>".$this->lang->line("INSTITUTE")."</th>";
				}
				echo								"<th>".$this->lang->line("COURSE")."</th>";
				foreach($majData["cat"] as $catData) {
					/* csak kiemeltek jelentjenek meg */
					if($catData["catHighlight"] == "I") {
						echo								"<th>".$catData["catName"]."</th>";
					}
				}
				echo								"<th>&nbsp;</th>";
				echo							"</tr>";
				echo						"</thead>";
				echo						"<tbody>";
				$elso = FALSE;
			}
			echo							"<tr>";
			if($ENTITY != "INS") {
				echo								"<td>".anchor("institutes/".$majData["insUrlName"], $majData["insName"])."</td>";
			}
			echo								"<td>".anchor("institutes/".$majData["insUrlName"]."/".$majData["majUrlName"], $majData["majName"])."</td>";
			foreach($majData["cat"] as $catData) {
				if($catData["catHighlight"] == "I") {
					echo								"<td>".Common::coalesce(array($catData["e2cInt"], $catData["e2cText"]))."</td>";
				}
			}
			echo								"<td class=\"last ws\">".anchor("institutes/".$majData["insUrlName"]."/".$majData["majUrlName"], ucfirst($this->lang->line("MORE")), "class=\"button green\"")."</td>";
			echo							"</tr>";
		}
		echo						"</tbody>";
		echo					"</table>";

		echo				"</section>";

		echo			"</article>";
	}

	echo		"</div>";

	$cnt = $getAvailablePrograms["cnt"];
	$aktPage = Common::coalesce(array($this->input->post("page"), 1));
	$pageSize = $this->config->item("availableProgramPageSize");
	$maxPage = floor($cnt/$pageSize);
	if($cnt > $pageSize) {

		//echo $ENTITY;
		if(!isset($url_name)) {
			switch($ENTITY) {
				case "COU": {
					$url_name = $getCountry[0]["couUrlName"];
				} break;
				case "CITY": {
					$url_name = $getCity[0]["cityUrlName"];
				} break;
				case "INS": {
					$url_name = $getInstitute[0]["insUrlName"];
				} break;
			}
		}
		//Common::pr($entName);

		echo	"
					<script type=\"text/javascript\">
						function jsPager(page) {
							$.ajax({
								type: 'POST',
								url: '".site_url()."tabmenu_refresh',
								data: {entity: '".$ENTITY."', url_name: '".$url_name."', program_type: '".$selectedProgType."', entName: '".$entName."', page: page},
								beforeSend: function(xhr) {
									run = true;
									$('#articleAvailablePrograms').html('<?=Common::ajaxWaitMessage()?>');
								}
							}).done(function ( data ) {
									$('#articleAvailablePrograms').html(data);
									run = false;
							});
						}
					</script>
					";

		echo	"<nav class=\"pager\">";

		if($aktPage == 1) {
			echo		"<span class=\"button prev\">".$this->lang->line("PREV")."</span>";
		} else {
			echo		"<a class=\"button prev\" onclick=\"jsPager('".($aktPage - 1)."');\">".$this->lang->line("PREV")."</a>";
		}

		// max 10 number
		echo		"<span class=\"pages\">";
		$dotdotdot = FALSE;
		for($i = 1; $i <= $maxPage; $i++) {

			if($i < 4 or $i > $maxPage-3 or ($i > $aktPage-3 and $i < $aktPage+3)) {
				$dotdotdot = FALSE;
				if($aktPage == $i) {
					echo			"<strong>".$i."</strong>";
				} else {
					echo			"<a onclick=\"jsPager('".$i."');\">".$i."</a>";
				}
			} else {
				if(!$dotdotdot) {
					echo	"...";
					$dotdotdot = TRUE;
				}
			}

		}
		echo		"</span>";

		if($maxPage == $aktPage) {
			echo		"<span class=\"button next\">".$this->lang->line("NEXT")."</span>";
		} else {
			echo		"<a class=\"button next\" onclick=\"jsPager('".($aktPage + 1)."');\">".$this->lang->line("NEXT")."</a>";
		}

		echo	"</nav>";
	}

	if(!$this->input->is_ajax_request()) echo	"</article>";

} else {
	echo		"<div class=\"pager\">".$this->lang->line("NOT_AVAILABLE_PROGRAM_TYPE")."</div>";
}

if($selectedProgType) {
	?>
	<script type="text/javascript">
		$(function(){
			if($('#<?=$selectedProgType?>').length) {
				$('html, body').animate({
					scrollTop: $('#<?=$selectedProgType?>').offset().top - 50
				}, 1000);
			}
		})
	</script>
	<?
}

//Common::pr($getAvailablePrograms);